import os
import base64
import dashscope
# 读取本地图片并编码为 base64
image_path = r"C:\Users\fangchen\Desktop\123.png"
with open(image_path, "rb") as image_file:
    encoded_str = base64.b64encode(image_file.read()).decode("utf-8")

# 构造 data URI
data_uri = f"data:image/jpeg;base64,{encoded_str}"
messages = [
    {
        "role": "user",
        "content": [
            {"image": data_uri},
            {"text": "帮我找出这个图片中日期2025-04-25,贷方发生额不为0的金额,并将金额以amount=xxx的格式输出"}
        ]
    }
]

response = dashscope.MultiModalConversation.call(
    # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key="sk-xxx"
    api_key="sk-3e0b22531704473698a248579130663d",
    model='qwen-vl-max-latest',  # 此处以qwen-vl-max-latest为例，可按需更换模型名称。模型列表：https://help.aliyun.com/model-studio/getting-started/models
    messages=messages,
    vl_high_resolution_images=True
)

print("大模型的回复:\n ",response.output.choices[0].message.content[0]["text"])
print("Token用量情况：","输入总Token：",response.usage["input_tokens"] , "，输入图像Token：" , response.usage["image_tokens"])